package stz.flash.main;

import net.rim.device.api.system.Bitmap;
import stz.flash.main.state.KeyOperator;
import blackberry.util.field.DjChangeableBitmapField;
import blackberry.util.image.DjImageResizer;

public class BulbField extends DjChangeableBitmapField {
	
	private static final int INDEX_OF_ON_BITMAP = 0;
	private static final int INDEX_OF_OFF_BITMAP = 1;
	
	private static final String IMAGE_ON = "Bulb_on.png";
	private static final String IMAGE_OFF = "Bulb_off.png";
	
	private static BulbState state;
	
	private static final BulbField instance = new BulbField();
	
	private BulbField() {
		super(new Bitmap[] {
				DjImageResizer.fitIntoDisplay(
						Bitmap.getBitmapResource(IMAGE_ON), 
						147, 242,
						ImageXY.WIDTH_OF_VIRTUAL_IMAGE, 
						ImageXY.HEIGHT_OF_VIRTUAL_IMAGE, 
						0, Bitmap.FILTER_BILINEAR),
				
						DjImageResizer.fitIntoDisplay(
								Bitmap.getBitmapResource(IMAGE_OFF), 
								147, 242, 
								ImageXY.WIDTH_OF_VIRTUAL_IMAGE, 
								ImageXY.HEIGHT_OF_VIRTUAL_IMAGE, 
								0, Bitmap.FILTER_BILINEAR)},
				NON_FOCUSABLE);
		
		
	}
	
	public static BulbField getIns(){
		return instance;
	}
	
	public void updateBulb() {
		state.updateBulb(this);
	}
	
	public void on(){
		changeBitmapField(INDEX_OF_ON_BITMAP);	
	}
	
	public void off(){
		changeBitmapField(INDEX_OF_OFF_BITMAP);	
	}
	
	public void changeStateAndUpdate(BulbState state){
		BulbField.state = state;
		updateBulb();
	}
	
	public void execute() {
		KeyOperator.runSpace();
	}
}

interface BulbState {
	public void updateBulb(BulbField bulbField);
}
